package com.vue.cn.utils.upload;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.servlet.http.Part;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
 * @title UploadUtils.java
 * @description  阿里云图片
 * @author pzy
 * @updateTime 2020/10/24 11:31
 */
@Component
@PropertySource("classpath:aliyun.properties")
public class UploadUtils {
    @Value("${endpoint}")
    private  String endPoint;
    @Value("${accessKeyId}")
    private  String accessKeyId;
    @Value("${accessKeySecret}")
    private  String accessKeySecret;
    @Value("${bucket}")
    private  String bucket;

    public  void fileUpLoad(String fileName, Part part) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucket, fileName, part.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    public void fileUpLoad(String fileName, byte[] content) {
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucket, fileName, new ByteArrayInputStream(content));
        // 关闭OSSClient。
        ossClient.shutdown();
    }
}
